/**
 * Encoding and hashing algorithms
 *
 * @author nasca iacob
 * @version 1.0.0
 *
 * All the algos are minified for the hell of it
 */
namespace("jclass.utils.encoders")({
    Base64 : jClass({
        public : {
            encode : function(input) {
                var o=new StringMaker(),a,b,c,d,f,g,h,i=0;while(i<input.length){a=input.charCodeAt(i++);
                b=input.charCodeAt(i++);c=input.charCodeAt(i++);d=a>>2;f=((a&3)<<4)|(b>>4);g=((b&15)<<2)|(c>>6);h=c&63;
                if(isNaN(b)){g=h=64;}else if(isNaN(c)){h=64;}o.append(keyStr.charAt(d)+keyStr.charAt(f)+keyStr.charAt(g)
                +keyStr.charAt(h));}return o.toString();
            },
            decode : function(input) {
                var o=new StringMaker(),a,b,c,d,e,f,g,i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i<input.length){
                d=keyStr.indexOf(input.charAt(i++));e=keyStr.indexOf(input.charAt(i++));f=keyStr.indexOf(input.charAt(i++));
                g=keyStr.indexOf(input.charAt(i++));a=(d<<2)|(e>>4);b=((e&15)<<4)|(f>>2);c=((f&3)<<6)|g;o.append(
                String.fromCharCode(a));if(f!=64){o.append(String.fromCharCode(b));}if(g!=64){o.append(String.fromCharCode(c));}}
                return o.toString();
            }
        },
        private : {
            keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
        }
    }),

    Huffman : jClass({
        public : {
            encode : function(input) {
                var l=new Array(256),lC=new Array(256),i;for(i=0;i<256;i++){l[i]=0;}for(i=0;i<input.length;i++)
                {l[input.charCodeAt(i)]++;}var nL=new Array(512),nC=new Array(512),nC1=new Array(512),
                nC2=new Array(512),nP=0;for(i=0;i<256;i++){if(l[i]>0){nL[nP]=i;nC[nP]=l[i];nC1[nP]=-1;nC2[nP]=-1;nP++;}}
                var sN2=1;while(sN2!=-1){var sN1=-1;sN2=-1;for(i=0;i<nP;i++){if(nC[i]>0){if(sN1==-1){sN1=i;}else if
                (sN2==-1){if(nC[i]<nC[sN1]){sN2=sN1;sN1=i;}else{sN2=i;}}else if(nC[i]<=nC[sN1]){sN2=sN1;sN1=i;}}}if(sN2!=-1)
                {nC[nP]=nC[sN1]+nC[sN2];nC[sN1]=0;nC[sN2]=0;nC1[nP]=sN2;nC2[nP]=sN1;nP++;}}var fN=Array(nP),dI=Array(256),
                d=0,nFinal=0;dI[d]=sN1;while(d>=0){if(nC1[dI[d]]>-1&&nC2[dI[d]]>-1){idx=nC1[dI[d]];nC1[dI[d]]=-2-nFinal;
                d++;dI[d]=idx;nFinal++;}else if(nC1[dI[d]]<0&&nC2[dI[d]]>-1){idx=nC1[dI[d]];idx=0-idx;idx-=2;fN[idx]=-nFinal;
                idx=nC2[dI[d]];nC2[dI[d]]=-2;d++;dI[d]=idx;}else if(nC1[dI[d]]<-1&&nC2[dI[d]]<-1){d--;}else if(nC1[dI[d]]
                ==-1&&nC2[dI[d]]==-1){fN[nFinal]=nL[dI[d]];nFinal++;d--;}else{throw('Bad algorithm!');return;}}
                var cI=new Array(256);dI[0]=0;cI[0]="";d=0;while(d>=0){if(fN[dI[d]]<0){var c=cI[d],idx=dI[d];dI[d+1]=dI[d]+1;
                cI[d+1]=c+'0';dI[d]=0-fN[idx];cI[d]=c+'1';d++;}else{lC[fN[dI[d]]]=cI[d];d--;}}var bits="",b=new StringMaker();
                for(i=0;i<input.length;i++){bits+=lC[input.charCodeAt(i)];while(bits.length>5){b.append(BitsToBytes(bits));
                bits=bits.slice(6,bits.length);}}b.append(BitsToBytes(bits));var result='',eN="";for(i=0;i<fN.length;i++)
                {var x,y;x=fN[i]+512;y=x&0x3F;x>>=6;x&=0x3F;x+=42;y+=42;if(x>=92){x++;}if(y>=92){y++;}eN+=String.fromCharCode(x)
                +String.fromCharCode(y);}result+=input.length+"+";result+='"'+b.toString()+"\";"+eN;return result;
            },
            decode : function(input) {
                var l=new Array(),c=input.match(/[0-9]+/)[0];input=input.substr(c.length+1);c=parseInt(c);
                var d=input.match(/"(.*)";/)[1];input=input.substr(d.length+3);if(input.charAt(0)==';')input=input.substr(1);
                while(input.length){l.push((Y(input.charCodeAt(0))<<6)+Y(input.charCodeAt(1))-512);input=input.slice(2,
                input.length)}var e=0,b=0,input=0;function Y(y){if(y>92)y--;return y-42}function B(){if(input==0){b=Y(
                d.charCodeAt(e++));input=6;}return((b>>--input)&0x01);}var result="";while(c--){var i=0;while(l[i]<0){
                if(B())i=-l[i];else i++;}result+=String.fromCharCode(l[i]);}return result;
            }
        },
        private : {
            MakeIntoString : function(S) {
                S=StringReplace("\\","\\\\",S);S=StringReplace("\"","\\\"",S);S=StringReplace("\n","\\n",S);return S;
            },
            BitsToBytes : function(i) {
                var o=42;if(i.charAt(0)=='1'){o+=32;}if(i.charAt(1)=='1'){o+=16;}if(i.charAt(2)=='1'){o+=8;}if(i.charAt(3)=='1')
                {o+=4;}if(i.charAt(4)=='1'){o+=2;}if(i.charAt(5)=='1'){o+=1;}if(o>=92){o++;}return String.fromCharCode(o);
            }

        }
    }),

    LZ78 : jClass({
        public : {
            encode : function(input) {
                var lc=new Array(),cl=1,cn=0,ct=1,o=new StringMaker(),b="",t_in=input,l,s,m=256,M=-1;
                for(var i=0;i<t_in.length;i++){var c=t_in.charCodeAt(i);if(m>c){m=c;}if(M<c){M=c;}}for(var i=m;i<=M;i++)
                {if(cn==ct){cl++;ct*=2;}lc[String.fromCharCode(i)]=cn++;}l="";s="";for(var i=0;i<t_in.length;i++){s+=
                t_in.charAt(i);if(lc[s]!==undefined){l=lc[s];}else{if(cn==ct){cl++;ct<<=1;}lc[s]=cn++;b+=BytesToBits(cl,l);
                while(b.length>=6){o.append(BitsToBytes(b));b=b.slice(6,b.length);}s=t_in.charAt(i);l=lc[s];}}if(cn==ct)
                {cl++;ct<<=1;}b+=BytesToBits(cl,l);while(b.length){o.append(BitsToBytes(b));b=b.slice(6,b.length);}
                o.prepend(m+"+"+M+";");return o.toString();
            },
            decode : function(input) {
                var tmp=input.substr(0,input.indexOf(';'));d=input.substr(tmp.length+1);
                var mincode=parseInt(tmp.split('+')[0]),maxcode=parseInt(tmp.split('+')[1]);var l,x,b,o,t,c,s,r,p,e;
                l=c=p=-1;x=b=e=0;o='';t=new Array();s=r=1;function g(){if(!b){if(x>=d.length)return-1;l=d.charCodeAt(x);
                if(l>92)l--;l-=42;x++;b=6;}return(l>>--b)&1;}function a(){var b,n=0,c=0;for(;n<s;n++)c=c*2+(b=g());
                return (b<0)?-1:c;}for(var i=mincode;i<=maxcode;i++){if(e+1==r){s++;r*=2;}t[e++]=String.fromCharCode(i);}
                while((c=a())>=0){if(p>=0){t[e++]=t[p]+(t[c]||'').charAt(0);}o+=t[c];p=c;if(t.length+1==r){r*=2;s++;}}return o;
            }
        },
        private : {
            BytesToBits : function(len, num) {
                var o="";while(len--){if(num&0x01){o="1"+o;}else{o="0"+o;}num>>=1;}return o;
            },
            BitsToBytes : function(i) {
	            var o=42;if(i.charAt(0)=='1'){o+=32;}if(i.charAt(1)=='1'){o+=16;}if(i.charAt(2)=='1'){o+=8;}
	            if(i.charAt(3)=='1'){o+=4;}if(i.charAt(4)=='1'){o+=2;}if(i.charAt(5)=='1'){o+=1;}if(o>=92){o++;}
	            return String.fromCharCode(o);
            }
        }
    }),

    MD5 : jClass({
        public : {
            hash : function(val) {
                return rstr2hex(rstr_md5(str2rstr_utf8(val)));
            }
        },
        private : {
            b64pad  : "",
            rstr_md5 : function(s) {
                return binl2rstr(binl_md5(rstr2binl(s),s.length*8));
            },
            rstr_hmac_md5 : function(key,data) {
                var bkey=rstr2binl(key);if(bkey.length>16)bkey=binl_md5(bkey,key.length*8);var ipad=Array(16),opad=
                Array(16);for(var i=0;i<16;i++){ipad[i]=bkey[i]^0x36363636;opad[i]=bkey[i]^0x5C5C5C5C;}var hash=binl_md5(
                ipad.concat(rstr2binl(data)),512+data.length*8);return binl2rstr(binl_md5(opad.concat(hash),512+128));
            },
            rstr2hex : function(input) {
                var hex_tab="0123456789abcdef";var output="",x;for(var i=0;i<input.length;i++){x=input.charCodeAt(i);
                output+=hex_tab.charAt((x>>>4)&0x0F)+hex_tab.charAt(x&0x0F);}return output;
            },
            str2rstr_utf8 : function(input) {
                var output="",i=-1,x,y;while(++i<input.length){x=input.charCodeAt(i);y=i+1<input.length?input.charCodeAt(i+1):0;
                if(0xD800<=x&&x<=0xDBFF&&0xDC00<=y&&y<=0xDFFF){x=0x10000+((x&0x03FF)<<10)+(y&0x03FF);i++;}if(x<=0x7F)
                output+=String.fromCharCode(x);else if(x<=0x7FF)output+=String.fromCharCode(0xC0 | ((x>>>6 )&0x1F),
                0x80|(x&0x3F));else if(x<=0xFFFF)output+=String.fromCharCode(0xE0|((x>>>12)&0x0F),0x80|((x>>>6 )&0x3F),
                0x80|(x&0x3F));else if(x<=0x1FFFFF)output+=String.fromCharCode(0xF0|((x>>>18)&0x07),0x80|((x>>>12)&0x3F),
                0x80|((x>>>6)&0x3F),0x80|(x&0x3F));}return output;
            },
            rstr2binl : function(input) {
                var output=Array(input.length>>2);for(var i=0;i<output.length;i++)output[i]=0;for(var i=0;i<input.length*8;i+=8)
                output[i>>5]|=(input.charCodeAt(i/8)&0xFF)<<(i%32);return output;
            },
            binl2rstr : function(input) {
                var o="";for(var i=0;i<input.length*32;i+=8)o+=String.fromCharCode((input[i>>5]>>>(i % 32))&0xFF);return o;
            },
            binl_md5 : function(x,len) {
                x[len>>5]|=0x80<<((len)%32);x[(((len+64)>>>9)<<4)+14]=len;var a=1732584193,b=-271733879,c=-1732584194,
                rol=function(num,cnt){return(num<<cnt)|(num>>>(32-cnt))},add=function(x,y){var lsw=(x&0xFFFF)+(y&0xFFFF),
                msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xFFFF)},cnm=function(q,a,b,x,s,t){return add(rol(add
                (add(a,q),add(x,t)),s),b)},ff=function(a,b,c,d,x,s,t){return cnm((b&c)|((~b)&d),a,b,x,s,t)},gg=function(
                a,b,c,d,x,s,t){return cnm((b&d)|(c&(~d)),a,b,x,s,t)},hh=function(a,b,c,d,x,s,t){return cnm(b^c^d,a,b,x,s,t)},
                ii=function(a,b,c,d,x,s,t){return cnm(c^(b|(~d)),a,b,x,s,t)},d=271733878;for(var i=0;i<x.length;i+=16){
                var olda=a,oldb=b,oldc=c,oldd=d;a=ff(a,b,c,d,x[i+0],7,-680876936);d=ff(d,a,b,c,x[i+1],12,-389564586);c=
                ff(c,d,a,b,x[i+2],17,606105819);b=ff(b,c,d,a,x[i+3],22,-1044525330);a=ff(a,b,c,d,x[i+4],7,-176418897);d=
                ff(d,a,b,c,x[i+5],12,1200080426);c=ff(c,d,a,b,x[i+6],17,-1473231341);b=ff(b,c,d,a,x[i+7],22,-45705983);a
                =ff(a,b,c,d,x[i+8],7,1770035416);d=ff(d,a,b,c,x[i+9],12,-1958414417);c=ff(c,d,a,b,x[i+10],17,-42063);b=
                ff(b,c,d,a,x[i+11],22,-1990404162);a=ff(a,b,c,d,x[i+12],7,1804603682);d=ff(d,a,b,c,x[i+13],12,-40341101);
                c=ff(c,d,a,b,x[i+14],17,-1502002290);b=ff(b,c,d,a,x[i+15],22,1236535329);a=gg(a,b,c,d,x[i+1],5,-165796510);
                d=gg(d,a,b,c,x[i+6],9,-1069501632);c=gg(c,d,a,b,x[i+11],14,643717713);b=gg(b,c,d,a,x[i+0],20,-373897302);
                a=gg(a,b,c,d,x[i+5],5,-701558691);d=gg(d,a,b,c,x[i+10],9,38016083);c=gg(c,d,a,b,x[i+15],14,-660478335);
                b=gg(b,c,d,a,x[i+4],20,-405537848);a=gg(a,b,c,d,x[i+9],5,568446438);d=gg(d,a,b,c,x[i+14],9,-1019803690);
                c=gg(c,d,a,b,x[i+3],14,-187363961);b=gg(b,c,d,a,x[i+8],20,1163531501);a=gg(a,b,c,d,x[i+13],5,-1444681467);
                d=gg(d,a,b,c,x[i+2],9,-51403784);c=gg(c,d,a,b,x[i+7],14,1735328473);b=gg(b,c,d,a,x[i+12],20,-1926607734);
                a=hh(a,b,c,d,x[i+5],4,-378558);d=hh(d,a,b,c,x[i+8],11,-2022574463);c=hh(c,d,a,b,x[i+11],16,1839030562);
                b=hh(b,c,d,a,x[i+14],23,-35309556);a=hh(a,b,c,d,x[i+1],4,-1530992060);d=hh(d,a,b,c,x[i+4],11,1272893353);
                c=hh(c,d,a,b,x[i+7],16,-155497632);b=hh(b,c,d,a,x[i+10],23,-1094730640);a=hh(a,b,c,d,x[i+13],4,681279174);
                d=hh(d,a,b,c,x[i+0],11,-358537222);c=hh(c,d,a,b,x[i+3],16,-722521979);b=hh(b,c,d,a,x[i+6],23,76029189);a=
                hh(a,b,c,d,x[i+9],4,-640364487);d=hh(d,a,b,c,x[i+12],11,-421815835);c=hh(c,d,a,b,x[i+15],16,530742520);b=
                hh(b,c,d,a,x[i+2],23,-995338651);a=ii(a,b,c,d,x[i+0],6,-198630844);d=ii(d,a,b,c,x[i+7],10,1126891415);c=
                ii(c,d,a,b,x[i+14],15,-1416354905);b=ii(b,c,d,a,x[i+5],21,-57434055);a=ii(a,b,c,d,x[i+12],6,1700485571);
                d=ii(d,a,b,c,x[i+3],10,-1894986606);c=ii(c,d,a,b,x[i+10],15,-1051523);b=ii(b,c,d,a,x[i+1],21,-2054922799);
                a=ii(a,b,c,d,x[i+8],6,1873313359);d=ii(d,a,b,c,x[i+15],10,-30611744);c=ii(c,d,a,b,x[i+6],15,-1560198380);
                b=ii(b,c,d,a,x[i+13],21,1309151649);a=ii(a,b,c,d,x[i+4],6,-145523070);d=ii(d,a,b,c,x[i+11],10,-1120210379);
                c=ii(c,d,a,b,x[i+2],15,718787259);b=ii(b,c,d,a,x[i+9],21,-343485551);a=add(a,olda);b=add(b,oldb);c=add(c,
                oldc);d=add(d,oldd);}return Array(a,b,c,d);
            }
        }
    }),

    SHA1 : jClass({
        public : {
            hash : function(s) {
                return rstr2hex(rstr_sha1(str2rstr_utf8(s)))
            }
        },
        private : {
            rstr_sha1 : function(s) {
                return binb2rstr(binb_sha1(rstr2binb(s),s.length*8));
            },
            rstr2hex : function(input) {
                var hex_tab="0123456789abcdef",output="",x;for(var i=0;i<input.length;i++){x=input.charCodeAt(i);output+=
                hex_tab.charAt((x>>>4)&0x0F)+hex_tab.charAt(x&0x0F);}return output;
            },
            str2rstr_utf8 : function(input) {
                var output="",i=-1,x,y;while(++i<input.length){x=input.charCodeAt(i);y=i+1<input.length?input.charCodeAt(i+1):0;
                if(0xD800<=x&&x<=0xDBFF&&0xDC00<=y&&y<=0xDFFF){x=0x10000+((x&0x03FF)<<10)+(y&0x03FF);i++;}if(x<=0x7F)
                output+=String.fromCharCode(x);else if(x<=0x7FF)output+=String.fromCharCode(0xC0|((x>>>6 )&0x1F),0x80|(x&
                0x3F));else if(x<=0xFFFF)output+=String.fromCharCode(0xE0|((x>>>12)&0x0F),0x80|((x>>>6 )&0x3F),0x80|(x&
                0x3F));else if(x<=0x1FFFFF)output+=String.fromCharCode(0xF0|((x>>>18)&0x07),0x80|((x>>>12)&0x3F),0x80|(
                (x>>>6)&0x3F),0x80|(x&0x3F));}return output;
            },
            rstr2binb : function(input) {
                var output=Array(input.length>>2);for(var i=0;i<output.length;i++)output[i]=0;for(var i=0;i<input.length
                *8;i+=8)output[i>>5]|=(input.charCodeAt(i/8)&0xFF)<<(24-i%32);return output;
            },
            binb2rstr : function(input) {
                var output="";for(var i=0;i<input.length*32;i+=8)output+=String.fromCharCode((input[i>>5]>>>(24-i%32))&0xFF);return output;
            },
            binb_sha1 : function(x,len) {
                x[len>>5]|=0x80<<(24-len%32);x[((len+64>>9)<<4)+15]=len;var rol=function(num,cnt){return(num<<cnt)|(num
                >>>(32-cnt));},add=function(x,y){var lsw=(x&0xFFFF)+(y&0xFFFF),msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)
                |(lsw&0xFFFF)},kt=function(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;},
                ft=function(t,b,c,d){if(t<20)return(b&c)|((~b)&d);if(t<40)return b^c^d;if(t<60)return(b&c)|(b&d)|(c&d);
                return b^c^d;},w=Array(80),a=1732584193,b=-271733879,c=-1732584194,d=271733878,e=-1009589776;for(var i=0;
                i<x.length;i+=16){var olda=a,oldb=b,oldc=c,oldd=d,olde=e;for(var j=0;j<80;j++){if(j<16)w[j]=x[i+j];else
                w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);var t=add(add(rol(a,5),ft(j,b,c,d)),add(add(e,w[j]),kt(j)));
                e=d;d=c;c=rol(b,30);b=a;a=t;}a=add(a,olda);b=add(b,oldb);c=add(c,oldc);d=add(d,oldd);e=add(e,olde);}
                return Array(a,b,c,d,e);
            }
        }
    })
});

if(navigator.userAgent.toLowerCase().indexOf(" chrome/")>=0||navigator.userAgent.toLowerCase().indexOf(" firefox/")>=0||
navigator.userAgent.toLowerCase().indexOf(' gecko/')>=0){var StringMaker=function(){this.str="";this.length=0;
this.append=function(s){this.str+=s;this.length+=s.length;};this.prepend=function(s){this.str=s+this.str;this.length+=s.length;};
this.toString=function(){return this.str;}}}else{var StringMaker=function(){this.parts=[];this.length=0;this.append=function(s){
this.parts.push(s);this.length+=s.length;};this.prepend=function(s){this.parts.unshift(s);this.length+=s.length;};
this.toString=function(){return this.parts.join('');}}}